package com.example.healthmanagement.service;

import com.example.healthmanagement.dao.*;
import com.example.healthmanagement.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.List;

// UserService.java
@Service
public class UserService {

    private final UserRepository userRepository;
    private final ChildInformationUserRepository childRepository;


    private final FoodInfoRepository foodInfoRepository;
    private final ChildDietaryRecordsRepository childDietaryRecordsRepository;

    private final SportInfoRepository sportInfoRepository;
    private final SportRecordsRepository sportRecordsRepository;

    private final ReportRepository reportRepository;
    private final ChildInformationHisRepository childInformationHisRepository;





    @Autowired
    public UserService(UserRepository userRepository, ChildInformationUserRepository childRepository, FoodInfoRepository foodInfoRepository, ChildDietaryRecordsRepository childDietaryRecordsRepository, SportInfoRepository sportInfoRepository, SportRecordsRepository sportRecordsRepository, ReportRepository reportRepository,ChildInformationHisRepository childInformationHisRepository) {
        this.userRepository = userRepository;
        this.childRepository = childRepository;
        this.foodInfoRepository = foodInfoRepository;
        this.childDietaryRecordsRepository = childDietaryRecordsRepository;
        this.sportInfoRepository = sportInfoRepository;
        this.sportRecordsRepository = sportRecordsRepository;
        this.reportRepository = reportRepository;
        this.childInformationHisRepository = childInformationHisRepository;
    }



    public List<User> getAllUsers() {
        return userRepository.findAll();
    }

    public boolean createUser(User user) {
        if (userRepository.existsByUsername(user.getUsername())) {
            return false;
        } else {
            userRepository.save(user);
            return true;
        }
    }

    public boolean checkUser(LoginForm loginInfo) {
        User user = userRepository.getUserByUsername(loginInfo.getUsername());
        if (user == null) {
            return false;
        }
        return user.getPassword().equals(loginInfo.getPassword());
    }

    public Integer getUserId(String userName) {
        return userRepository.getUserByUsername(userName).getUserID();
    }

    public void changePassword(User user) {
        User user1 = userRepository.getUserByUsername(user.getUsername());
        user1.setPassword(user.getPassword());
        userRepository.save(user1);
    }

    public String getPassword(User user) {
        return userRepository.getUserByUsername(user.getUsername()).getPassword();
    }

    public void addChild(Integer userID, ChildInformation childInformation) {
        User user = userRepository.getUserByUserID(userID);
        childInformation.setUser(user);
        //获取当前日期作为CreateTime
        //控制器显示获取当前日期

        LocalDateTime time= LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = time.format(formatter);

        childInformation.setCreatetime(formattedTime);

        DecimalFormat decimalFormat = new DecimalFormat("#.#");

        // 使用 DecimalFormat 格式化 float 值
        String formattedValue = decimalFormat.format(childInformation.getWeight()/(childInformation.getHeight()/100*childInformation.getHeight()/100));
        childInformation.setBmi(formattedValue);

        childRepository.save(childInformation);
    }

    public void addChildInformationHis(ChildInformationHis childInformationHisService ){
        childInformationHisRepository.save(childInformationHisService);
    }

    public List<ChildInformation> getChildList(Integer userID) {
        return childRepository.getChildInformationsByUser_UserIDIs(userID);
    }

    public List<ChildInformationHis> getChildHisList() {
        return childInformationHisRepository.findAll();
    }

    public ChildInformation getChildByID(Integer ChildID) {
        return childRepository.getChildInformationByChildID(ChildID);
    }

    public List<FoodInfo> getFoodInfo() {
        return foodInfoRepository.findAll();
    }

    public List<SportInfo> getSportInfo() {
        return sportInfoRepository.findAll();
    }

    public ChildDietaryRecords saveDietaryRecords(ChildDietaryRecords childDietaryRecords) {
        return childDietaryRecordsRepository.save(childDietaryRecords);
    }

    public ChildSportRecords saveSportRecords(ChildSportRecords sportRecords) {
        return sportRecordsRepository.save(sportRecords);
    }

    public List<ChildDietaryRecords> getDietaryListByChildID(Integer childID) {
        return childDietaryRecordsRepository.findAllByChildInformation_ChildID(childID);
    }

    public List<ChildSportRecords> getSportListByChildID(Integer childID) {
        return sportRecordsRepository.findAllByChildInformation_ChildID(childID);
    }

    public boolean saveReport(Report report, Integer childID) {
        List<Report> old = reportRepository.getReportByChildInformation_ChildID(childID);
        for (Report report1 : old) {
            if (isToday(report1.getCreateTime().split(" ")[0])) {
                reportRepository.delete(report1);
            }
        }

        reportRepository.save(report);
        return true;
    }

    public List<Report> getReport(Integer childID){
        return reportRepository.getReportByChildInformation_ChildID(childID);
    }

    public boolean isToday(String str) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();

        // 将日期字符串解析为 LocalDate 对象
        String dateString = str;  // 替换为你的日期字符串
        LocalDate parsedDate = LocalDate.parse(dateString, DateTimeFormatter.ISO_DATE);

        // 判断是否为今天
        return currentDate.equals(parsedDate);
    }


    public User getUserById(String userId) {
        User userByUsername = userRepository.getUserByUserID(Integer.valueOf(userId));
        return userByUsername;
    }

    public boolean updateUser(User user) {
        User userByUserID = userRepository.getUserByUserID(user.getUserID());
        if(null != userByUserID){
            //userRepository.delete(user);
            User save = userRepository.save(user);
            return true;
        }
        return false;
    }

    public void deleteByUserID(User user) {
        userRepository.delete(user);
    }

    public List<Report> findSport(Integer childID) {
        return reportRepository.getReportByChildInformation_ChildID(childID);
    }

    public void updateReport(Report report) {
        reportRepository.save(report);
    }

    public void addChildInFormationHis(ChildInformationHis childInformationHis){
       childInformationHisRepository.save(childInformationHis);
    }
}